library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(data.table)
library(chron)
## NOTE: The default cutoff when expanding a 2-digit year
## to a 4-digit year will change from 30 to 69 by Aug 2020
## (as for Date and POSIXct in base R.)
library(ellipsis)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:data.table':
## 
##     first, last
library(zoo)
library(stats)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(corrr)
library(corrplot)
## corrplot 0.84 loaded
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------- tidyverse 1.3.0 --
## <U+221A> tibble  3.0.4     <U+221A> purrr   0.3.4
## <U+221A> tidyr   1.1.2     <U+221A> stringr 1.4.0
## <U+221A> readr   1.4.0     <U+221A> forcats 0.5.0
## -- Conflicts ----------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::between()   masks data.table::between()
## x dplyr::filter()    masks plotly::filter(), stats::filter()
## x dplyr::first()     masks xts::first(), data.table::first()
## x dplyr::lag()       masks stats::lag()
## x dplyr::last()      masks xts::last(), data.table::last()
## x purrr::transpose() masks data.table::transpose()
library(miscTools)
library("factoextra")
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library("ggplot2")

setwd("C:/Users/kadir/OneDrive/Masaüstü")

trainx<-fread("uWaveGestureLibrary_X_TRAIN")
trainy<-fread("uWaveGestureLibrary_Y_TRAIN")
trainz<-fread("uWaveGestureLibrary_Z_TRAIN")


trainx<-data.frame(trainx)
trainy<-data.frame(trainy)
trainz<-data.frame(trainz)

TASK 1

tx<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    tx[(i-1)*315+j]<-trainx[i,j+1]
    
    
  }
}

ty<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    ty[(i-1)*315+j]<-trainy[i,j+1]
    
    
  }
}


tz<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    tz[(i-1)*315+j]<-trainz[i,j+1]
    
    
  }
}


class<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    class[(i-1)*315+j]<-trainz[i,1]
    
    
  }
}

timeindex<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    timeindex[(i-1)*315+j]<-j
    
    
  }
}

timeseriesid<-vector(mode = "numeric", length = 282240)

for(i in 1:896){
  for(j in 1:315){
    timeseriesid[(i-1)*315+j]<-i
    
    
  }
}

traintable<-data.table(timeseriesid,timeindex,tx,ty,tz,class)
traintable
##         timeseriesid timeindex         tx         ty         tz class
##      1:            1         1 -0.3042432 -2.1193958 -1.5289651     6
##      2:            1         2 -0.3042432 -2.1193958 -1.5289651     6
##      3:            1         3 -0.3042432 -2.1193958 -1.5289651     6
##      4:            1         4 -0.3042432 -2.1193958 -1.5289651     6
##      5:            1         5 -0.3042432 -2.1193958 -1.5289651     6
##     ---                                                              
## 282236:          896       311  1.9685751 -0.8867771 -0.8486020     5
## 282237:          896       312  2.0285319 -0.8206512 -0.8281042     5
## 282238:          896       313  1.9872671 -0.7545253 -0.8004260     5
## 282239:          896       314  1.8875777 -0.6883994 -0.7686033     5
## 282240:          896       315  1.7878883 -0.6222735 -0.7367806     5
ttx<-t(tx)
ttx<-cumsum(ttx)


tty<-t(ty)
tty<-cumsum(tty)

ttz<-t(tz)
ttz<-cumsum(ttz)


positionx<-t(ttx)
positionx<-cumsum(positionx)


positiony<-t(tty)
positiony<-cumsum(positiony)

positionz<-t(ttz)
positionz<-cumsum(positionz)



figclass1 <- plot_ly(x = positionx[3151:3465], y =positiony[3151:3465], z =positionz[3151:3465])
figclass1 <- figclass1  %>% layout(title = 'Class1')
figclass1
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
figclass2 <- plot_ly(x = positionx[4411:4725], y =positiony[4411:4725], z =positionz[4411:4725])
figclass2 <- figclass2  %>% layout(title = 'Class2')
figclass2
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass3 <- plot_ly(x = positionx[946:1260], y =positiony[946:1260], z =positionz[946:1260])
figclass3 <- figclass3  %>% layout(title = 'Class3')
figclass3
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass4 <- plot_ly(x = positionx[1261:1575], y =positiony[1261:1575], z =positionz[1261:1575])
figclass4 <- figclass4  %>% layout(title = 'Class4')
figclass4
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass5 <- plot_ly(x = positionx[316:630], y =positiony[316:630], z =positionz[316:630])
figclass5 <- figclass5  %>% layout(title = 'Class5')
figclass5
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass6 <- plot_ly(x = positionx[1:315], y =positiony[1:315], z =positionz[1:315])
figclass6 <- figclass6  %>% layout(title = 'Class6')
figclass6
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass7 <- plot_ly(x = positionx[1:315], y =positiony[1:315], z =positionz[1:315])
figclass7 <- figclass7  %>% layout(title = 'Class7')
figclass7
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
figclass8 <- plot_ly(x = positionx[1576:1890], y =positiony[1576:1890], z =positionz[1576:1890])
figclass8  <- figclass8  %>% layout(title = 'Class8')
figclass8
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

###Time series for each class is visualized above.

TASK 2

traintable2<-copy(traintable[,3:5])



pca1 <- princomp(traintable2, cor=T)
summary(pca1, loadings=T)
## Importance of components:
##                          Comp.1    Comp.2    Comp.3
## Standard deviation     1.213171 1.0198583 0.6986445
## Proportion of Variance 0.490595 0.3467037 0.1627014
## Cumulative Proportion  0.490595 0.8372986 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.427  0.776  0.464
## ty  0.721        -0.692
## tz  0.546 -0.630  0.553
traintable3<-data.table(timeseriesid,traintable2)
pca2 <- princomp(traintable3, cor=T)
summary(pca2, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2   Comp.3    Comp.4
## Standard deviation     1.2131714 1.0198583 1.000000 0.6986445
## Proportion of Variance 0.3679462 0.2600277 0.250000 0.1220260
## Cumulative Proportion  0.3679462 0.6279740 0.877974 1.0000000
## 
## Loadings:
##              Comp.1 Comp.2 Comp.3 Comp.4
## timeseriesid                1.000       
## tx            0.427  0.776        -0.464
## ty            0.721                0.692
## tz            0.546 -0.630        -0.553
gesture1<-copy(filter(traintable,class==1))

gesture2<-copy(filter(traintable,class==2))

gesture3<-copy(filter(traintable,class==3))

gesture4<-copy(filter(traintable,class==4))

gesture5<-copy(filter(traintable,class==5))

gesture6<-copy(filter(traintable,class==6))

gesture7<-copy(filter(traintable,class==7))

gesture8<-copy(filter(traintable,class==8))


sample1<-copy(filter(traintable,class==1 & timeseriesid==182 ))

sample2<-copy(filter(traintable,class==1 & timeseriesid==495 ))

sample1[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample2[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class1deneme<-data.table(sample1$pca1,sample2$pca1)
class1deneme[,time:=1:315]
setnames(class1deneme, old = "V1", new = "first")
setnames(class1deneme, old = "V2", new = "second")

ggplot(class1deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class1")+ylab("Pca Value")

sample3<-copy(filter(traintable,class==2 & timeseriesid==170 ))

sample4<-copy(filter(traintable,class==2 & timeseriesid==323 ))

sample3[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample4[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class2deneme<-data.table(sample3$pca1,sample4$pca1)
class2deneme[,time:=1:315]
setnames(class2deneme, old = "V1", new = "first")
setnames(class2deneme, old = "V2", new = "second")


ggplot(class2deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class2")+ylab("Pca Value")

sample5<-copy(filter(traintable,class==3 & timeseriesid==350))

sample6<-copy(filter(traintable,class==3 & timeseriesid==892))

sample5[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample6[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class3deneme<-data.table(sample5$pca1,sample6$pca1)
class3deneme[,time:=1:315]
setnames(class3deneme, old = "V1", new = "first")
setnames(class3deneme, old = "V2", new = "second")


ggplot(class3deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class3")+ylab("Pca Value")

sample7<-copy(filter(traintable,class==4 & timeseriesid==317))

sample8<-copy(filter(traintable,class==4 & timeseriesid==580))

sample7[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample8[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class4deneme<-data.table(sample7$pca1,sample8$pca1)
class4deneme[,time:=1:315]
setnames(class4deneme, old = "V1", new = "first")
setnames(class4deneme, old = "V2", new = "second")

ggplot(class4deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class4")+ylab("Pca Value")

sample9<-copy(filter(traintable,class==5 & timeseriesid==69))

sample10<-copy(filter(traintable,class==5 & timeseriesid==332))

sample9[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample10[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class5deneme<-data.table(sample9$pca1,sample10$pca1)
class5deneme[,time:=1:315]
setnames(class5deneme, old = "V1", new = "first")
setnames(class5deneme, old = "V2", new = "second")

ggplot(class5deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class5")+ylab("Pca Value")

sample11<-copy(filter(traintable,class==6 & timeseriesid==207))

sample12<-copy(filter(traintable,class==6 & timeseriesid==631))

sample11[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample12[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class6deneme<-data.table(sample11$pca1,sample12$pca1)
class6deneme[,time:=1:315]
setnames(class6deneme, old = "V1", new = "first")
setnames(class6deneme, old = "V2", new = "second")

ggplot(class6deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class6")+ylab("Pca Value")

sample13<-copy(filter(traintable,class==7 & timeseriesid==417))

sample14<-copy(filter(traintable,class==7 & timeseriesid==613))

sample13[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample14[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class7deneme<-data.table(sample13$pca1,sample14$pca1)
class7deneme[,time:=1:315]
setnames(class7deneme, old = "V1", new = "first")
setnames(class7deneme, old = "V2", new = "second")

ggplot(class7deneme, aes(time)) + 
geom_line(aes(y = first, colour = "var0")) + 
geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class7")+ylab("Pca Value")

sample15<-copy(filter(traintable,class==8 & timeseriesid==6))

sample16<-copy(filter(traintable,class==8 & timeseriesid==440))

sample15[,pca1:=0.427*tx+0.721*ty+0.546*tz]
sample16[,pca1:=0.427*tx+0.721*ty+0.546*tz]

class8deneme<-data.table(sample15$pca1,sample16$pca1)
class8deneme[,time:=1:315]
setnames(class8deneme, old = "V1", new = "first")
setnames(class8deneme, old = "V2", new = "second")

ggplot(class8deneme, aes(time)) + 
  geom_line(aes(y = first, colour = "var0")) + 
  geom_line(aes(y =  second, colour = "var1"))+ggtitle("Two Sample from Class8")+ylab("Pca Value")

###The two time series I chose for each class follow each other in harmony as can be seen in the graphs above.

TASK 3

pcaclass1 <- princomp(gesture1[,3:5], cor=T)
summary(pcaclass1, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3
## Standard deviation     1.1779640 0.9845807 0.8018738
## Proportion of Variance 0.4625331 0.3231331 0.2143339
## Cumulative Proportion  0.4625331 0.7856661 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.357  0.896  0.263
## ty  0.691        -0.720
## tz  0.629 -0.439  0.642
pcaclass2 <- princomp(gesture2[,3:5], cor=T)
summary(pcaclass2, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3
## Standard deviation     1.2400176 0.9682706 0.7244366
## Proportion of Variance 0.5125479 0.3125160 0.1749361
## Cumulative Proportion  0.5125479 0.8250639 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.455  0.801  0.388
## ty  0.685        -0.728
## tz  0.569 -0.597  0.565
pcaclass3 <- princomp(gesture3[,3:5], cor=T)
summary(pcaclass3, loadings=T)
## Importance of components:
##                          Comp.1    Comp.2    Comp.3
## Standard deviation     1.273669 0.9475424 0.6927707
## Proportion of Variance 0.540744 0.2992789 0.1599771
## Cumulative Proportion  0.540744 0.8400229 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.675         0.738
## ty  0.531  0.689 -0.494
## tz -0.513  0.725  0.460
pcaclass4 <- princomp(gesture4[,3:5], cor=T)
summary(pcaclass4, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3
## Standard deviation     1.2846735 0.9605566 0.6534102
## Proportion of Variance 0.5501287 0.3075563 0.1423150
## Cumulative Proportion  0.5501287 0.8576850 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.681  0.113  0.724
## ty  0.634  0.404 -0.659
## tz -0.367  0.908  0.204
pcaclass5 <- princomp(gesture5[,3:5], cor=T)
summary(pcaclass5, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2     Comp.3
## Standard deviation     1.3934383 0.9076507 0.48425204
## Proportion of Variance 0.6472234 0.2746099 0.07816668
## Cumulative Proportion  0.6472234 0.9218333 1.00000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.399  0.915       
## ty  0.643 -0.321  0.696
## tz  0.654 -0.243 -0.716
pcaclass6 <- princomp(gesture6[,3:5], cor=T)
summary(pcaclass6, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2     Comp.3
## Standard deviation     1.3097601 0.9941695 0.54420161
## Proportion of Variance 0.5718239 0.3294577 0.09871846
## Cumulative Proportion  0.5718239 0.9012815 1.00000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.206  0.964  0.167
## ty -0.680  0.264 -0.684
## tz -0.704         0.710
pcaclass7 <- princomp(gesture7[,3:5], cor=T)
summary(pcaclass7, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2    Comp.3
## Standard deviation     1.2480424 1.0144419 0.6428823
## Proportion of Variance 0.5192033 0.3430308 0.1377659
## Cumulative Proportion  0.5192033 0.8622341 1.0000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.229  0.925  0.304
## ty  0.715        -0.697
## tz  0.661 -0.377  0.649
pcaclass8 <- princomp(gesture8[,3:5], cor=T)
summary(pcaclass8, loadings=T)
## Importance of components:
##                           Comp.1    Comp.2     Comp.3
## Standard deviation     1.3565694 0.9675745 0.47277805
## Proportion of Variance 0.6134269 0.3120668 0.07450636
## Cumulative Proportion  0.6134269 0.9254936 1.00000000
## 
## Loadings:
##    Comp.1 Comp.2 Comp.3
## tx  0.574  0.584  0.574
## ty  0.693        -0.721
## tz  0.436 -0.811  0.389
pcatable<-data.table(matrix(0,9,3))



pcatable[1,1]=summary(pcaclass1, loadings=T)$loadings[1,1]
pcatable[1,2]=summary(pcaclass1, loadings=T)$loadings[2,1]
pcatable[1,3]=summary(pcaclass1, loadings=T)$loadings[3,1]


pcatable[2,1]=summary(pcaclass2, loadings=T)$loadings[1,1]
pcatable[2,2]=summary(pcaclass2, loadings=T)$loadings[2,1]
pcatable[2,3]=summary(pcaclass2, loadings=T)$loadings[3,1]

pcatable[3,1]=summary(pcaclass3, loadings=T)$loadings[1,1]
pcatable[3,2]=summary(pcaclass3, loadings=T)$loadings[2,1]
pcatable[3,3]=summary(pcaclass3, loadings=T)$loadings[3,1]

pcatable[4,1]=summary(pcaclass4, loadings=T)$loadings[1,1]
pcatable[4,2]=summary(pcaclass4, loadings=T)$loadings[2,1]
pcatable[4,3]=summary(pcaclass4, loadings=T)$loadings[3,1]

pcatable[5,1]=summary(pcaclass5, loadings=T)$loadings[1,1]
pcatable[5,2]=summary(pcaclass5, loadings=T)$loadings[2,1]
pcatable[5,3]=summary(pcaclass5, loadings=T)$loadings[3,1]

pcatable[6,1]=summary(pcaclass6, loadings=T)$loadings[1,1]
pcatable[6,2]=summary(pcaclass6, loadings=T)$loadings[2,1]
pcatable[6,3]=summary(pcaclass6, loadings=T)$loadings[3,1]

pcatable[7,1]=summary(pcaclass7, loadings=T)$loadings[1,1]
pcatable[7,2]=summary(pcaclass7, loadings=T)$loadings[2,1]
pcatable[7,3]=summary(pcaclass7, loadings=T)$loadings[3,1]

pcatable[8,1]=summary(pcaclass8, loadings=T)$loadings[1,1]
pcatable[8,2]=summary(pcaclass8, loadings=T)$loadings[2,1]
pcatable[8,3]=summary(pcaclass8, loadings=T)$loadings[3,1]

pcatable[9,1]=summary(pca1, loadings=T)$loadings[1,1]
pcatable[9,2]=summary(pca1, loadings=T)$loadings[2,1]
pcatable[9,3]=summary(pca1, loadings=T)$loadings[3,1]

setnames(pcatable, old = "V1", new = "coefficientx")
setnames(pcatable, old = "V2", new = "coefficienty")
setnames(pcatable, old = "V3", new = "coefficientz")

pcatable
##    coefficientx coefficienty coefficientz
## 1:    0.3567943    0.6911427    0.6285058
## 2:    0.4554176    0.6845547    0.5691921
## 3:    0.6747233    0.5306881   -0.5129510
## 4:    0.6806323    0.6342065   -0.3667721
## 5:    0.3988772    0.6425900    0.6541980
## 6:    0.2060269   -0.6797230   -0.7039386
## 7:    0.2290300    0.7150258    0.6605175
## 8:    0.5744344    0.6927923    0.4359633
## 9:    0.4268455    0.7212650    0.5455087

###PC1 of 1st, 2nd and 7th classes are similar to each other.Y component has the largest coefficient while x component has the smallest coefficient.The same situation is valid for PC1 of all data.There are differences in PC1 of other classes.(9th row of pcatable represents PC1 of all data)

TASK 4

mdstrain<-cbind(trainx,trainy[,2:316],trainz[,2:316])




d <- dist(mdstrain)
fit <- cmdscale(d,eig=TRUE, k=2)

x <- fit$points[,1]
y <- fit$points[,2]


mds_appliedts <- cbind(x,y,trainx[,1])
mds_appliedts<-data.table(mds_appliedts)
setnames(mds_appliedts, old = "V3", new = "class")
mds_appliedts[,timeseriesid:=1:896]


mds_appliedts$class = as.character(mds_appliedts$class)


ggplot(mds_appliedts,aes(x=x,y=y,color=class))+geom_point() +labs(title = " MDS ", x="Coordinate 1", y="Coordinate 2")

###Observations of each class seem to be clustered in a specific area.